const dayjs = require('dayjs')
const Service = require('egg').Service

class TaskService extends Service {
  async dayTask(date = dayjs().format('YYYY-MM-DD')) {
    const { ctx } = this
    const { query } = ctx

    const returnData = {}

    const onError = e => console.error(e)

    await Promise.all([
      this.service.trackData
        .townPositive()
        .then(res => (returnData['乡镇阳性'] = res))
        .catch(onError),
      this.service.trackData
        .townPerson()
        .then(res => (returnData['乡镇人员'] = res))
        .catch(onError),
      this.service.trackData
        .placeType()
        .then(res => (returnData['场所分类'] = res))
        .catch(onError),

      this.service.trackData
        .placeTypeScanByScope('day', date)
        .then(res => (returnData['日场所分类扫码'] = res))
        .catch(onError),
      this.service.trackData
        .placeTypeScanByScope('week', date)
        .then(res => (returnData['周场所分类扫码'] = res))
        .catch(onError),
      this.service.trackData
        .placeTypeScanByScope('month', date)
        .then(res => (returnData['月场所分类扫码'] = res))
        .catch(onError),

      this.service.trackData
        .personEnterByScope('week', date)
        .then(res => (returnData['周人员流入'] = res))
        .catch(onError),
      this.service.trackData
        .personEnterByScope('month', date)
        .then(res => (returnData['月人员流入'] = res))
        .catch(onError),

      this.service.trackData
        .townHsResultByScope('day', date)
        .then(res => (returnData['天核酸结果'] = res))
        .catch(onError),
      this.service.trackData
        .townHsResultByScope('week', date)
        .then(res => (returnData['周核酸结果'] = res))
        .catch(onError),
      this.service.trackData
        .townHsResultByScope('month', date)
        .then(res => (returnData['月核酸结果'] = res))
        .catch(onError),

      this.service.trackData
        .placeScanByScope('day', date)
        .then(res => (returnData['天场所扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .placeScanByScope('week', date)
        .then(res => (returnData['周场所扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .placeScanByScope('month', date)
        .then(res => (returnData['月场所扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .placeScnPersonAgeByScope('day', date)
        .then(res => (returnData['天场所扫码人员年龄'] = res))
        .catch(onError),
      this.service.trackData
        .placeScnPersonAgeByScope('week', date)
        .then(res => (returnData['周场所扫码人员年龄'] = res))
        .catch(onError),
      this.service.trackData
        .placeScnPersonAgeByScope('month', date)
        .then(res => (returnData['月场所扫码人员年龄'] = res))
        .catch(onError),
      this.service.trackData
        .townScanByScope('day', date)
        .then(res => (returnData['天乡镇扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .townScanByScope('week', date)
        .then(res => (returnData['周乡镇扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .townScanByScope('month', date)
        .then(res => (returnData['月乡镇扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .scanNumByScope('day', date)
        .then(res => (returnData['天全县扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .scanNumByScope('week', date)
        .then(res => (returnData['周全县扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .scanNumByScope('month', date)
        .then(res => (returnData['月全县扫码数据'] = res))
        .catch(onError),
      this.service.trackData
        .townPlaceCode('day', date)
        .then(res => (returnData['乡镇场所码'] = res))
        .catch(onError),
      this.service.trackData
        .probablyPerson()
        .then(res => (returnData['密接人员'] = res))
        .catch(onError),
      this.service.trackData
        .personFromCityByScope('day', date)
        .then(res => (returnData['天返乡人员来源地'] = res))
        .catch(onError),
      this.service.trackData
        .personFromCityByScope('week', date)
        .then(res => (returnData['周返乡人员来源地'] = res))
        .catch(onError),
      this.service.trackData
        .personFromCityByScope('month', date)
        .then(res => (returnData['月返乡人员来源地'] = res))
        .catch(onError),

      this.service.trackData
        .townPersonEnterByScope('day', date)
        .then(res => (returnData['天乡镇人员流入'] = res))
        .catch(onError),
      this.service.trackData
        .townPersonEnterByScope('week', date)
        .then(res => (returnData['周乡镇人员流入'] = res))
        .catch(onError),
      this.service.trackData
        .townPersonEnterByScope('month', date)
        .then(res => (returnData['月乡镇人员流入'] = res))
        .catch(onError),

      this.service.trackData
        .personAge()
        .then(res => (returnData['人员年龄段'] = res))
        .catch(onError),
      this.service.trackData
        .townSpecialPerson()
        .then(res => (returnData['乡镇特殊人群'] = res))
        .catch(onError),
      this.service.trackData
        .personCategory()
        .then(res => (returnData['人口分类数据'] = res))
        .catch(onError),
      this.service.trackData
        .enterTrendByScope('day', date)
        .then(res => (returnData['天返乡外来人口趋势'] = res))
        .catch(onError),
      this.service.trackData
        .enterTrendByScope('week', date)
        .then(res => (returnData['周返乡外来人口趋势'] = res))
        .catch(onError),
      this.service.trackData
        .enterTrendByScope('month', date)
        .then(res => (returnData['月返乡外来人口趋势'] = res))
        .catch(onError),

      this.service.trackData
        .reportByScope('day', date)
        .then(res => (returnData['天返乡报备信息'] = res))
        .catch(onError),
      this.service.trackData
        .reportByScope('week', date)
        .then(res => (returnData['周返乡报备信息'] = res))
        .catch(onError),
      this.service.trackData
        .reportByScope('month', date)
        .then(res => (returnData['月返乡报备信息'] = res))
        .catch(onError),

      this.service.trackData
        .homeInfo('day', date)
        .then(res => (returnData['态势总览信息'] = res))
        .catch(onError),
      this.service.trackData
        .codeInfo()
        .then(res => (returnData['场所码'] = res))
        .catch(onError),
      this.service.trackData
        .hsInfo()
        .then(res => (returnData['核酸检测'] = res))
        .catch(onError),

      this.service.trackData
        .fxryPlaceScanByScope('day', date)
        .then(res => (returnData['天返乡人员场所扫码'] = res))
        .catch(onError),
      this.service.trackData
        .fxryPlaceScanByScope('week', date)
        .then(res => (returnData['周返乡人员场所扫码'] = res))
        .catch(onError),
      this.service.trackData
        .fxryPlaceScanByScope('month', date)
        .then(res => (returnData['月返乡人员场所扫码'] = res))
        .catch(onError),

      this.service.trackData
        .fxryJzdByScope('day', date)
        .then(res => (returnData['天返乡人员居住地'] = res))
        .catch(onError),
      this.service.trackData
        .fxryJzdByScope('week', date)
        .then(res => (returnData['周返乡人员居住地'] = res))
        .catch(onError),
      this.service.trackData
        .fxryJzdByScope('month', date)
        .then(res => (returnData['月返乡人员居住地'] = res))
        .catch(onError),

      this.service.trackData
        .hsyj()
        .then(res => (returnData['返乡人员核酸预警'] = res))
        .catch(onError),

      this.service.trackData
        .fxyj()
        .then(res => (returnData['风险预警'] = res))
        .catch(onError),

      this.service.trackData
        .reportInfo()
        .then(res => (returnData['返乡报备'] = res))
        .catch(onError),

      this.service.trackData
        .townHsCollect()
        .then(res => (returnData['乡镇核酸采集'] = res))
        .catch(onError),

      this.service.trackData
        .syncFxyr()
        .then(res => (returnData['同步返乡人员'] = res))
        .catch(onError),
    ])

    await this.service.trackData.getFxryScanData()
    await this.service.trackData.getFxryHsData()

    return returnData
  }
}

module.exports = TaskService
